<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\models\Article\Article;
use common\models\Article\ArticleCategory;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\Article\ArticleSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Articles');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="article-index box box-primary">
    <?= $this->render('@app/views/layouts/_tab.php') ?>
    <div class="box-header with-border">
        <?php if (\mdm\admin\components\Helper::checkRoute('create')) { ?>
            <?= Html::a(Yii::t('app', 'Create'), ['create'], ['class' => 'btn btn-success btn-flat']) ?>
        <?php } ?>
    </div>
    <div class="box-body table-responsive">
        <?php //echo $this->render('_search', ['model' => $searchModel]); ?>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'layout' => "{items}\n{summary}\n{pager}",
            'columns' => [
                [
                    'label' => 'ID',
                    'format' => 'raw',
                    'attribute' => 'id',
                    'headerOptions' => ["style" => ['width' => '50px']],
                    'value' => function ($model) {
                        return $model->id;
                    }
                ],
                [
                    'label' => '标题',
                    'format' => 'raw',
                    'attribute' => 'title',
                    'value' => function ($model) {
                        return $model->title;
                    },
                ],
                [
                    'label' => '文章分类',
                    'format' => 'raw',
                    'attribute' => 'pid',
                    'filter' => Html::dropDownList('ArticleSearch[pid]', $searchModel->pid,
                        ['' => '全部'] + ArticleCategory::getListForOptions(), ['class' => 'form-control']),
                    'value' => function ($model) {
                        return ArticleCategory::getFieldById($model->pid, 'name');
                    }
                ],
                [
                    'label' => '状态',
                    'format' => 'raw',
                    'attribute' => 'status',
                    'filter' => Html::dropDownList('ArticleSearch[status]', $searchModel->status,
                        ['' => '全部'] + Article::getStatusOptions(), ['class' => 'form-control']),
                    'value' => function ($model) {
                        return Article::getStatusName($model->status);
                    }
                ],
//                [
//                    'label' => '内容',
//                    'format' => 'raw',
//                    'attribute' => 'content',
//                    'value' => function ($model) {
//                        return StringHelper::truncate($model->content, 10, '...', null, true);
//                    }
//                ],
                [
                    'label' => '更新时间',
                    'format' => 'datetime',
                    'attribute' => 'updated_at',
                    'value' => function ($model) {
                        return $model->updated_at;
                    }
                ],
                // 'created_at',
                // 'updated_at',

                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => \mdm\admin\components\Helper::filterActionColumn('{update}{delete}'),
                    'buttons' => [
                        'update' => function ($url, $model, $key) {
                            return \yii\helpers\Html::a("<i class='fa fa-fw fa-edit'></i>编辑", ['update', 'id' => $key], ['class' => 'btn btn-primary']);
                        },
                        'delete' => function ($url, $model, $key) {
                            return \yii\helpers\Html::a("<i class='fa fa-fw fa-recycle'></i>删除", ['delete', 'id' => $key],
                                [
                                    'class' => 'btn btn-primary',
                                    'data' => [
                                        'confirm' => Yii::t('app', '是否确认删除?'),
                                        'method' => 'post'
                                    ]
                                ]);
                        }],
                ],
            ],
        ]); ?>
    </div>
</div>
